package hierarchy

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/hierarchy"
	hierarchyReq "gitcode.com/G-YT/iHealth25/server/model/hierarchy/request"
	"gitcode.com/G-YT/iHealth25/server/service"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type FdArticleLearnApi struct {
}

var fdArticleLearnService = service.ServiceGroupApp.HierarchyServiceGroup.FdArticleLearnService

//var UserService = service.ServiceGroupApp.UserServiceGroup.UserService

// CreateFdArticleLearn 创建FdArticleLearn
// @Tags FdArticleLearn
// @Summary 创建FdArticleLearn
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleLearn true "创建FdArticleLearn"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /fdArticleLearn/createFdArticleLearn [post]
func (fdArticleLearnApi *FdArticleLearnApi) CreateFdArticleLearn(c *gin.Context) {
	var fdArticleLearn hierarchy.FdArticleLearn
	err := c.ShouldBindJSON(&fdArticleLearn)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleLearnService.CreateFdArticleLearn(&fdArticleLearn); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteFdArticleLearn 删除学习文章
// @Tags FdArticleLearn
// @Summary 删除学习文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleVideo true "删除学习文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /fdArticleLearn/deleteFdArticleLearn [delete]
func (fdArticleLearnApi *FdArticleLearnApi) DeleteFdArticleLearn(c *gin.Context) {
	var fdArticleLearn hierarchy.FdArticleLearn
	err := c.ShouldBindJSON(&fdArticleLearn)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleLearnService.DeleteFdArticleLearn(fdArticleLearn); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteFdArticleLearnByIds 批量删除学习文章
// @Tags FdArticleLearn
// @Summary 批量删除学习文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除学习文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除学习文章"}"
// @Router /fdArticleVideo/deleteFdArticleLearnByIds [delete]
func (fdArticleLearnApi *FdArticleLearnApi) DeleteFdArticleLearnByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleLearnService.DeleteFdArticleLearnByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateFdArticleLearn 更新学习文章
// @Tags FdArticleLearn
// @Summary 更新学习文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleLearn true "更新学习文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /fdArticleLearn/updateFdArticleLearn [put]
func (fdArticleLearnApi *FdArticleLearnApi) UpdateFdArticleLearn(c *gin.Context) {
	var fdArticleLearn hierarchy.FdArticleLearn
	err := c.ShouldBindJSON(&fdArticleLearn)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleLearnService.UpdateFdArticleLearn(fdArticleLearn); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindFdArticleLearn 用id查询学习文章
// @Tags FdArticleVideo
// @Summary 用id查询学习文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchy.FdArticleLearn true "用id查询学习文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /fdArticleLearn/findFdArticleLearn [get]
func (fdArticleLearnApi *FdArticleLearnApi) FindFdArticleLearn(c *gin.Context) {
	var fdArticleLearn hierarchy.FdArticleLearn
	err := c.ShouldBindQuery(&fdArticleLearn)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetHmID(c)
	if refdArticleLearn, err := fdArticleLearnService.GetFdArticleLearn(fdArticleLearn.ID, userID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"refdArticleLearn": refdArticleLearn}, c)
	}
}

// GetFdArticleLearnList 分页获取学习文章
// @Tags FdArticleLearn
// @Summary 分页获取学习文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.FdArticleLearnSearch true "分页获取学习文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /fdArticleLearn/getFdArticleLearnList [get]
func (fdArticleLearnApi *FdArticleLearnApi) GetFdArticleLearnList(c *gin.Context) {
	var pageInfo hierarchyReq.FdArticleLearnSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := fdArticleLearnService.GetFdArticleLearnInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// FdArticleLike 点赞&取消点赞
// @Tags FdArticleLearn
// @Summary 点赞&取消点赞
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"点赞&取消点赞成功"}"
// @Router /fdArticleLearn/like [get]
func (fdArticleLearnApi *FdArticleLearnApi) FdArticleLike(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.Atoi(id)
	userID := utils.GetUserID(c)
	if refdArticleLearn, err := fdArticleLearnService.FdArticleLike(userID, uint(uid)); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		err = UserService.AddPoints(userID, 1, 1)
		response.OkWithData(gin.H{"refdArticleLearn": refdArticleLearn}, c)
	}
}

// FdArticleCollect 收藏&取消收藏
// @Tags FdArticleLearn
// @Summary 收藏&取消收藏
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"收藏&取消收藏成功"}"
// @Router /fdArticleLearn/collect [get]
func (fdArticleLearnApi *FdArticleLearnApi) FdArticleCollect(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.Atoi(id)
	userID := utils.GetUserID(c)
	if refdArticleLearn, err := fdArticleLearnService.FdArticleCollect(userID, uint(uid)); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"refdArticleLearn": refdArticleLearn}, c)
	}
}

// SetBuyArticle 设置购买学习文章
// @Tags FdArticleLearn
// @Summary 设置购买学习文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"设置购买学习文章"}"
// @Router /fdArticleLearn/setBugArticle [get]
func (fdArticleLearnApi *FdArticleLearnApi) SetBuyArticle(c *gin.Context) {
	var req hierarchyReq.BuyArticleReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleLearnService.SetBuyArticle(req); err != nil {
		global.GVA_LOG.Error("设置失败!", zap.Error(err))
		response.FailWithMessage("设置失败", c)
	} else {
		response.OkWithMessage("设置成功", c)
	}
}

// GetBuyArticle 获取购买学习文章
// @Tags FdArticleLearn
// @Summary 获取购买学习文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取购买学习文章"}"
// @Router /fdArticleLearn/getBuyArticle [get]
func (fdArticleLearnApi *FdArticleLearnApi) GetBuyArticle(c *gin.Context) {
	hmID := c.Query("hm_id")
	if res, err := fdArticleLearnService.GetBuyArticle(hmID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(res, c)
	}
}

// GetLearnLevel 获取学习目录【健管师端】
// @Tags FdArticleLearn
// @Summary 获取学习目录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取学习目录"}"
// @Router /fdArticleLearn/getLearnLevel [get]
func (fdArticleLearnApi *FdArticleLearnApi) GetLearnLevel(c *gin.Context) {
	levelOneID := c.Query("level_one_id")
	if res, err := fdArticleLearnService.GetLearnLevel(levelOneID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(res, c)
	}
}

// GetLearnArticleByHmId 获取学习文章列表【健管师端】
// @Tags FdArticleLearn
// @Summary 获取学习文章列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取学习文章列表"}"
// @Router /fdArticleLearn/getLearnArticleByHmId [get]
func (fdArticleLearnApi *FdArticleLearnApi) GetLearnArticleByHmId(c *gin.Context) {
	levelThreeID := c.Query("level_three_id")
	hmID := utils.GetHmID(c)
	if res, err := fdArticleLearnService.GetLearnArticleByHmId(hmID, levelThreeID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(res, c)
	}
}
